package net.alegen.tracker;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class PathOverlay 
	extends Overlay {

	private final List<GeoPoint> pathPoints;
	
	Paint greenPaint;
	Point pointOne, pointTwo;
	RectF rectangle;
	
	public PathOverlay(List<GeoPoint> pathPoints) {
		super();
		this.pathPoints = pathPoints;
		this.pointOne = new Point();
		this.pointTwo = new Point();
		this.rectangle = new RectF();
		this.greenPaint = new Paint();
		this.greenPaint.setARGB(255, 0, 255, 0);
		this.greenPaint.setStrokeWidth(5.0f);
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    	if(!this.pathPoints.isEmpty()) {
    		synchronized (this.pathPoints) {
	    		pointOne = mapView.getProjection().toPixels(this.pathPoints.get(0), null);
	    		this.rectangle.set(this.pointOne.x - 10,
	    				this.pointOne.y - 10,
	    				this.pointOne.x + 10,
	    				this.pointOne.y + 10);
	    		canvas.drawOval(this.rectangle, this.greenPaint);
	    		for(int i = 1; i < this.pathPoints.size(); i++) {
	    			pointTwo = mapView.getProjection().toPixels(this.pathPoints.get(i), null);
	    			canvas.drawLine(this.pointOne.x, 
	    					this.pointOne.y, 
	    					this.pointTwo.x, 
	    					this.pointTwo.y, 
	    					this.greenPaint);
	    			this.pointOne = this.pointTwo;
	    		}
    		}
    	}
	}
}
